import 'dart:async';

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:hxdj/common/routers/base_routers.dart';
import 'dart:convert' as convert;


/// fluro的路由跳转工具类
class NavigatorUtils {

  static Future<void> push(
    BuildContext context,
    String path, {
    bool replace = false,
    bool clearStack = false,
    TransitionType transition = TransitionType.cupertino
    ,bool noAnimation=false
  }) {
    unfocus(); 
    return Routes.router.navigateTo(context, path,
        replace: replace, clearStack: clearStack, transition: noAnimation?TransitionType.none:transition);
  }

  static void pushResult(
    BuildContext context,
    String path,
    Function(Object) function, {
    bool replace = false,
    bool clearStack = false,
    TransitionType transition = TransitionType.cupertino,
  }) {
    unfocus();
    Routes.router
        .navigateTo(context, path,
            replace: replace, clearStack: clearStack, transition: transition)
        .then((Object result) {
      // 页面返回result为null
      function(result);
    } as FutureOr Function(dynamic value)).catchError((dynamic error) {
      debugPrint('$error');
    });
  }

  /// 带future路由
  static Future navigateTo(
      BuildContext context,
      String routeName, {
        Object? object,
        bool replace = false,
        bool clearStack = false,
        bool maintainState = true,
        bool rootNavigator = false,
        TransitionType transition =  TransitionType.cupertino,
        Duration? transitionDuration,
        required Widget Function(BuildContext, Animation<double>, Animation<double>, Widget)
        transitionBuilder,
      }) {
    // ignore: omit_local_variable_types
    RouteSettings? routeSettings =
    object != null ? RouteSettings(arguments: object) : null;
    return Routes.router.navigateTo(context, routeName,
        transition: transition,
        replace: replace,
        clearStack: clearStack,
        maintainState: maintainState,
        rootNavigator: rootNavigator,
        transitionDuration: transitionDuration,
        transitionBuilder: transitionBuilder,
        routeSettings: routeSettings);
  }

  /// 返回
  static void goBack(BuildContext context) {
    unfocus();
    Navigator.maybePop(context);
  }

  ///返回到固定的页面
  ///context : 上下文
  ///path : 页面路径
  static void popUntil(
    BuildContext context,
    String path,
  ) {
    unfocus();
    Navigator.popUntil(context, (route) => route.settings.name == path);
  }

  /// 带参数返回
  static void goBackWithParams(BuildContext context, Object result) {
    unfocus();
    Navigator.pop<Object>(context, result);
  }



  static void unfocus() {
    // 使用下面的方式，会触发不必要的build。
    // FocusScope.of(context).unfocus();
    // https://github.com/flutter/flutter/issues/47128#issuecomment-627551073
    FocusManager.instance.primaryFocus?.unfocus();
  }

   ///带参数的传值
  ///context  上下文
  ///path     路由地址
  ///params  跳转携带的参数(路径中携带)
  ///arguments  跳转携带的参数
  ///transition  跳转动画
  ///replace  是否关闭当前页面
  ///clearStack  是否清除栈顶的类
  static Future pushParams(BuildContext context, String path,
      {Map<String, dynamic>? params,
      Function(Object)? function,
      Object? arguments,
      TransitionType transition = TransitionType.cupertino,
      bool replace = false,
      bool clearStack = false}) {
    unfocus();
    String query = "";
    if (params != null) {
      int index = 0;
      for (var key in params.keys) {
        var value = Uri.encodeComponent(params[key] is String?params[key]:(convert.jsonEncode(params[key])));
        if (index == 0) {
          query = "?";
        } else {
          query = "$query&";
        }
        query += "$key=$value";
        index++;
      }
    }
    path = path + query;
    return Routes.router
        .navigateTo(context, path,
            replace: replace,
            clearStack: clearStack,
            routeSettings: RouteSettings(arguments: arguments),
            transition: transition)
        .then((value) {
      if (function != null) {
        function(value);
      }
    });
  }

  static Future<void> pushWithArguments(
    BuildContext context,
    String path, {
    bool replace = false,
    bool clearStack = false,
    TransitionType transition = TransitionType.cupertino,
    dynamic arguments,
    Function(Object)? function,
  }) {
    if (arguments is String) {
      return pushParams(context, path,
          replace: replace,
          clearStack: clearStack,
          transition: transition,
          params: {"data": arguments},
          function: function);
    } else if (arguments is bool) {
      return pushParams(context, path,
          replace: replace,
          clearStack: clearStack,
          transition: transition,
          params: {"data": "$arguments"},
          function: function);
    } else {
      //map,List,Object
      String jsonEncode = convert.jsonEncode(arguments);
      return pushParams(context, path,
          replace: replace,
          clearStack: clearStack,
          transition: transition,
          params: {"data": jsonEncode},
          function: function);
    }
  }
}
